package com.metel.mservice.common.utils;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;

public class CRMLogUtil {
	
	public static enum LOG_TYPE{
		REQ, RES, ING
	}
	
	public static enum API_TYPE{
		PUBLIC, PRIVATE
	}
	
	public static enum STATUS {
		START_API, ING_API, END_API, 
		BEFORE_DAO_CALL, AFTER_DAO_CALL, 
		BEFORE_FILE_CALL, AFTER_FILE_CALL,
		BEFORE_BUDDY_CALL, AFTER_BUDDY_CALL,
		BEFORE_MSG_CALL, AFTER_MSG_CALL,
		BEFORE_SMS_CALL, AFTER_SMS_CALL,
		BEFORE_GLD_CALL, AFTER_GLD_CALL,
		BEFORE_OSP_CALL, AFTER_OSP_CALL,
		PUSH_EVENT_TIMEOUT
	}
	
	public static final String OK = "200";

	
	public static String getCrmLog(LOG_TYPE logType, String method, STATUS status, Object tracelog) {
		
		StringBuilder sb = new StringBuilder(SystemInfo.privateAddress + " | " + logType.toString()).append(" || \"");
		
		sb.append("method=").append(method);
		sb.append(",status=").append(status);
		sb.append(",tracelog=").append(tracelog);
		
		sb.append("\"");
		
		return sb.toString();
	}
	
	public static String getCrmLog(LOG_TYPE logType, String method, STATUS status, String uri, Object tracelog) {
		
		StringBuilder sb = new StringBuilder(SystemInfo.privateAddress + " | " + logType.toString()).append(" || \"");
		
		sb.append("method=").append(method);
		sb.append(",status=").append(status);
		sb.append(",api=").append(uri);
		sb.append(",tracelog=").append(tracelog);
		
		sb.append("\"");
		
		return sb.toString();
	}
	
	public static String getCrmLog(LOG_TYPE logType, String method, STATUS status, String appid, String chatonid, Object tracelog) {
		
		StringBuilder sb = new StringBuilder(SystemInfo.privateAddress + " | " + logType.toString()).append(" || \"");
		
		sb.append("method=").append(method);
		sb.append(",status=").append(status);
		sb.append(",access_key=").append(appid);
		sb.append(",chatonid=").append(chatonid);
		sb.append(",tracelog=").append(tracelog);

		sb.append("\"");
		
		return sb.toString();
	}
	
	public static String getCrmLog(LOG_TYPE logType, String method, STATUS status, String uri, String appid, String access_token, Object tracelog) {
		
		StringBuilder sb = new StringBuilder(SystemInfo.privateAddress + " | " + logType.toString()).append(" || \"");
		
		sb.append("method=").append(method);
		sb.append(",status=").append(status);
		sb.append(",api=").append(uri);
		sb.append(",access_key=").append(appid);
		sb.append(",access_token=").append(access_token);
		sb.append(",tracelog=").append(tracelog);

		sb.append("\"");
		
		return sb.toString();
	}

	/**
	 * 
	 * Biz 서버에서 Request 찍는 용도..
	 * 
	 * @param logType - REQ | RES | ING
	 * @param apiType - public | private
	 * @param url
	 * @param appid
	 * @param chatonid
	 * @param code - 200 | COP-00001 | xxx
	 * @return
	 */
	public static String getCrmRequestLog(LOG_TYPE logType, String method, STATUS status, API_TYPE apiType, String uri, String appid, String access_token, String enc_token) {
		
		StringBuilder sb = new StringBuilder(SystemInfo.privateAddress + " | " + logType.toString()).append(" || \"");
		
		sb.append("method=").append(method);
		sb.append(",status=").append(status);
		if(API_TYPE.PUBLIC == apiType) {
			sb.append(",api=/" + API_TYPE.PUBLIC).append(uri);			
		} else {
			sb.append(",api=/" + API_TYPE.PRIVATE).append(uri);
		}
		sb.append(",access_key=").append(appid);
		sb.append(",access_token=").append(access_token);
		sb.append(",enc_token=").append(enc_token);

		sb.append("\"");
		
		return sb.toString();
	}
	
	/**
	 * 
	 * @param logType - REQ | RES | ING
	 * @param apiType - public | private
	 * @param url
	 * @param appid
	 * @param chatonid
	 * @param code - 200 | COP-00001 | xxx
	 * @return
	 */
	public static String getCrmResponseLog(LOG_TYPE logType, STATUS status, API_TYPE apiType, HttpServletRequest request, String appid, String chatonid, String code) {
		
		StringBuilder sb = new StringBuilder(SystemInfo.privateAddress + " | " + logType.toString()).append(" || \"");
		
		sb.append("status=").append(status);		
		String url = request.getRequestURI();
		if(API_TYPE.PUBLIC == apiType) {
			sb.append(",api=/" + API_TYPE.PUBLIC).append(url);			
		} else {
			sb.append(",api=/" + API_TYPE.PRIVATE).append(url);
		}		
		sb.append(",access_key=").append(appid);
		sb.append(",chatonid=").append(chatonid);
		sb.append(",code=").append(code);

		sb.append("\"");
		
		return sb.toString();
	}

	public static String getCrmMsgLog(LOG_TYPE logType, String method, STATUS status, String appid, String chatonid, String chattype, 
			String msgtype, String msgid, String sessionid) {
		
		StringBuilder sb = new StringBuilder(SystemInfo.privateAddress + " | " + logType.toString()).append(" || \"");
		
		sb.append("method=").append(method);
		sb.append(",status=").append(status);
		sb.append(",access_key=").append(appid);
		sb.append(",chatonid=").append(chatonid);
		sb.append(",chattype=").append(chattype);
		sb.append(",msgtype=").append(msgtype);
		sb.append(",msgid=").append(msgid);
		sb.append(",sessionid=").append(sessionid);
		
		sb.append("\"");
		
		return sb.toString();
	}
	
	public static String getCrmMsgLog(LOG_TYPE logType, String method, STATUS status, String appid, String chatonid, String chattype, 
			String msgtype, String msgid, String sessionid, Object tracelog) {
		
		StringBuilder sb = new StringBuilder(SystemInfo.privateAddress + " | " + logType.toString()).append(" || \"");
		
		sb.append("method=").append(method);
		sb.append(",status=").append(status);
		sb.append(",access_key=").append(appid);
		sb.append(",chatonid=").append(chatonid);
		sb.append(",chattype=").append(chattype);
		sb.append(",msgtype=").append(msgtype);
		sb.append(",msgid=").append(msgid);
		sb.append(",sessionid=").append(sessionid);
		sb.append(",tracelog=").append(tracelog);
		
		sb.append("\"");
		
		return sb.toString();
	}
	
	public static String getCrmGraphLog(LOG_TYPE logType, String method, STATUS status, String appid, String chatonid) {
		
		
		StringBuilder sb = new StringBuilder(SystemInfo.privateAddress + " | " + logType.toString()).append(" || \"");
		
		sb.append("method=").append(method);
		sb.append(",status=").append(status.toString());
		sb.append(",appid=").append(appid);
		sb.append(",chatonid=").append(chatonid);
		
		sb.append("\"");
		
		return sb.toString();
	}
	
	public static String getCrmGraphLog(LOG_TYPE logType, String method, STATUS status, String appid, String chatonid, String webchatonid) {
		
		
		StringBuilder sb = new StringBuilder(SystemInfo.privateAddress + " | " + logType.toString()).append(" || \"");
		
		sb.append("method=").append(method);
		sb.append(",status=").append(status.toString());
		sb.append(",appid=").append(appid);
		sb.append(",chatonid=").append(chatonid);
		
		if(!StringUtils.isBlank(webchatonid)) {
			sb.append(",webchatonid=").append(webchatonid);
		}
		
		sb.append("\"");
		
		return sb.toString();
	}
	
	public static String getCrmGraphLog(LOG_TYPE logType, String method, STATUS status, String appid, String chatonid, String webchatonid, String url) {
		
		
		StringBuilder sb = new StringBuilder(SystemInfo.privateAddress + " | " + logType.toString()).append(" || \"");
		
		sb.append("method=").append(method);
		sb.append(",status=").append(status.toString());
		sb.append(",appid=").append(appid);
		sb.append(",chatonid=").append(chatonid);
		
		if(!StringUtils.isBlank(webchatonid)) {
			sb.append(",webchatonid=").append(webchatonid);
		}
		
		sb.append(",url=").append(url);
		
		sb.append("\"");
		
		return sb.toString();
	}
	
	public static String getCrmGraphLog(LOG_TYPE logType, String method, STATUS status, String appid, String chatonid, String webchatonid, Object obj, String size) {
		
		
		StringBuilder sb = new StringBuilder(SystemInfo.privateAddress + " | " + logType.toString()).append(" || \"");
		
		sb.append("method=").append(method);
		sb.append(",status=").append(status.toString());
		sb.append(",appid=").append(appid);
		sb.append(",chatonid=").append(chatonid);
		
		if(!StringUtils.isBlank(webchatonid)) {
			sb.append(",webchatonid=").append(webchatonid);
		}
		
		if(obj!=null) {
			sb.append(",obj=").append(obj);
		}
		
		if(!StringUtils.isBlank(size)) {
			sb.append(",size=").append(size);
		}
		
		sb.append("\"");
		
		return sb.toString();
	}
}